<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>#If</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<link href="../static/theme.css" rel="stylesheet" type="text/css" />
<script src="../static/content.js" type="text/javascript"></script>
</head>
<body>


<h1>#If <span class="ver">[AHK_L]</span></h1>

<p>Creates context-sensitive <a href="../Hotkeys.htm">hotkeys</a> and <a href="../Hotstrings.htm">hotstrings</a>. Such hotkeys perform a different action (or none at all) depending on the result of an expression.</p>

<pre class="Syntax">#If [, Expression ]</pre>
<h3>Parameters</h3>
<dl>

  <dt>Expression</dt>
  <dd><p>Any valid <a href="../Variables.htm#Expressions">expression</a>.</p></dd>

</dl>

<h3>Basic Operation</h3>
<p>Any valid expression may be used to define the context in which a hotkey should be active. For example:</p>
<pre>#If WinActive("ahk_class Notepad") or WinActive(MyWindowTitle)
#Space::MsgBox You pressed Win+Spacebar in Notepad or %MyWindowTitle%.</pre>
<p>Like the #IfWin directives, #If is positional: it affects all hotkeys and hotstrings physically beneath it in the script. #If and #IfWin are also mutually exclusive; that is, only the most recent #If or #IfWin will be in effect.</p>
<p>To turn off context sensitivity, specify #If or any #IfWin directive but omit all the parameters. For example:</p>
<pre>#If</pre>

<h3>General Remarks</h3>
<p>When the key combination which forms a hotkey is pressed, the #If expression is evaluated to determine if the hotkey should activate. The system may not respond to keyboard or mouse input until expression evaluation completes or <a href="_IfTimeout.htm">times out</a>. Sending keystrokes or mouse clicks while the expression is being evaluated (such as from a function which it calls) may cause complications and should be avoided.</p>
<p>The expression may also be evaluated whenever the program needs to know whether the hotkey is active. For example, the #If expression for a custom combination like <code>a &amp; b::</code> might be evaluated when the prefix key (<code>a</code> in this example) is pressed, to determine whether it should act as a custom modifier key.</p>
<p>For the reasons described above, the expression should be written to complete quickly and without side-effects.</p>
<p><span class="ver">[AHK_L 53+]:</span> <a href="../Variables.htm#ThisHotkey">A_ThisHotkey</a> and <a href="../Variables.htm#TimeSinceThisHotkey">A_TimeSinceThisHotkey</a> are set based on the hotkey for which the current #If expression is being evaluated.</p>
<p><span class="ver">[v1.0.95.00+]:</span> <a href="../Variables.htm#PriorHotkey">A_PriorHotkey</a> and <a href="../Variables.htm#TimeSincePriorHotkey">A_TimeSincePriorHotkey</a> temporarily contain the previous values of the corresponding "This" variables.</p>

<h3>Related</h3>
<p>Most behavioural properties of the <a href="_IfWinActive.htm">#IfWin</a> directives also apply to #If.</p>
<p><a href="_IfTimeout.htm">#IfTimeout</a> may be used to override the default timeout value.</p>

<h3 id="Examples">Examples</h3>
<pre class="NoIndent">
<em>; Example 1: Adjust volume by scrolling the mouse wheel over the taskbar.</em>
#If MouseIsOver("ahk_class Shell_TrayWnd")
WheelUp::Send {Volume_Up}
WheelDown::Send {Volume_Down}

MouseIsOver(WinTitle) {
    MouseGetPos,,, Win
    return WinExist(WinTitle . " ahk_id " . Win)
}

<em>; Example 2: Simple word-delete shortcuts for all Edit controls.</em>
#If ActiveControlIsOfClass("Edit")
^BS::Send ^+{Left}{Del}
^Del::Send ^+{Right}{Del}

ActiveControlIsOfClass(Class) {
    ControlGetFocus, FocusedControl, A
    ControlGet, FocusedControlHwnd, Hwnd,, %FocusedControl%, A
    WinGetClass, FocusedControlClass, ahk_id %FocusedControlHwnd%
    return (FocusedControlClass=Class)
}

<em>; Example 3: Context-insensitive hotkey.</em>
#If
Esc::ExitApp

<em id="ex4">; Example 4: Dynamic hotkeys. <b>Requires Example 1.</b></em>
NumpadAdd::
Hotkey, If, MouseIsOver("ahk_class Shell_TrayWnd")
if (doubleup := !doubleup)
    Hotkey, WheelUp, DoubleUp
else
    Hotkey, WheelUp, WheelUp
return

DoubleUp:
Send {Volume_Up 2}
return
</pre>

</body>
</html>
